package com.itheima.demo;

import com.itheima.domain.Emp;
import com.itheima.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

public class Demo1 {
    public static void main(String[] args)throws Exception {
       //1、读取核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2、构建SqlSessionFactory对象
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
        //3、获取SqlSession对象
        SqlSession session = factory.openSession();
        //直接调用statement对象
      //  List<Object> list1 = session.selectList("com.itheima.mapper.EmpMapper.findAll");

        //4、通过SqlSession获取Mapper代理对象(JDK动态代理)
        EmpMapper mapper = session.getMapper(EmpMapper.class);
        //5、调用接口中的方法
        List<Emp> list = mapper.findAll();
        System.out.println(list);
        //6、事务提交
        session.commit();
        //7、释放资源
        is.close();
        session.close();
    }
}
